#!/bin/sh
########################################################################
# common.dlg - a collection of common dialog
# Copyright (C) 2013 Mohamad Afifi Omar <afifio@gmail.com>
#
# These comments cannot be remove or edited and should remain intact
# because this file comments is an extension to the GPL3 license.
# This software file is covered by GPL version 3 license.
# Please see the LICENSE and gpl3.txt or gpl3.htm in doc directory
# for the license agreement, if its not exist, write to the 
# Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
# Boston, MA 02110-1301, USA.
# Modification below this line is allowed
########################################################################
# Extra credits to:
#
########################################################################
# Notes:
# These common dialog made as external GtkDialog program to set and
# provide a standard common dialogs, reduce clutter in your program,
# reduce code, time to develop etc. You can choose not to use it though.
# These dialog run on separate gtkdialog session. The most important is
# providing your prog name. All data will be look on standard location
# that is:
# Systemwide: /usr/share/$PROGNAME/[whatever]
# User: $HOME/.config/$PROGNAME/[whatever]
# MsgBox will look for its text in 
# $HOME/.config/$PROGNAME/tmp/[error|warning|information|question|authentication]
# you can echo your text there.
#
# Available dialog:
# Common MsgBox like Warning, Error, Information, Authentication, About
# External program - gfontsel, gcolor2
########################################################################
export COMMONDLGVERSION=20120208
if [ -z "$1" ]       # No arg passed.
then
	echo "Usage: `basename $0`  MsgBox ProgramName Argument"
	exit 1
fi

#define gtkdialog
if [ "`which gtkdialog4`" ]; then
 GTKDIALOG=gtkdialog4
elif [ "`which gtkdialog3`" ]; then
 GTKDIALOG=gtkdialog3
else
 GTKDIALOG=gtkdialog
fi
#echo $*
DialogType=$1
PROGNAME=$2
Argument=$3
PROGNAME=$2
if [ $DialogType = "Font" ]
then
	gfontsel &
elif [ $DialogType = "Color" ]
then
	gcolor2 &
elif [ $DialogType = "Preferences" ]
then
	export COMMONDIALOG='
<window title=" Preferences " icon-name="gtk-preferences">
<vbox>
<vbox space-expand="false" space-fill="false">
'"$(H3 gtk-preferences Preferences "New settings will be applied after program restart.")"'
</vbox>
<vbox space-expand="true" space-fill="true">
<frame>
<hbox space-expand="true" space-fill="true">
<hbox space-expand="false" space-fill="false">
	<vbox space-expand="false" space-fill="false" scrollable="true">
		'"$(prefsection)"'
	</vbox>
</hbox>
<hbox space-expand="true" space-fill="true">
<vbox space-expand="true" space-fill="true">
    <notebook labels="Info|Credits|Credits" show-tabs="false" show-border="false" space-expand="true" space-fill="true">
		'"$(prefpage)"'
		<input file>'"$PROGTMP"'/preference</input>
		<variable>PreferenceNotebook</variable>
    </notebook>
    </vbox>
</hbox>
</hbox>
</frame>
</vbox>
<vbox space-expand="false" space-fill="false">
    <button ok><action type="exit">COMMONDIALOG</action></button>
</vbox>
</vbox>
</window>
'	
	$GTKDIALOG --center --program=COMMONDIALOG

elif [ $DialogType = "MsgBox" ]
then
	if [ $Argument = "error" ]
	then
		title=Error
		icon=dialog-$Argument
		color=DarkRed
	elif [ $Argument = "info" ]
	then
		title=Information
		icon=dialog-$Argument
		color=CornflowerBlue
	elif [ $Argument = "warning" ]
	then
		title=Warning
		icon=dialog-$Argument
		color=DarkOrange
	elif [ $Argument = "question" ]
	then
		title=Question
		icon=dialog-$Argument
		color=DarkGreen
	elif [ $Argument = "authentication" ]
	then
		title=Authentication
		icon=dialog-$Argument
		color=DarkGray
	fi
	export COMMONDIALOG='
	<window title=" '"$title"' " icon-name="gtk-'"$icon"'">
	<vbox>
		<frame>
			<hbox>
				<pixmap icon_size="6">
					<input file stock="gtk-'$icon'"></input>
				</pixmap>
				<vbox space-expand="true" space-fill="true">
				<text use-markup="true" xalign="0">
				<label>"<span  color='"'$color'"' font-family='"'Sans'"' weight='"'bold'"' size='"'x-large'"'><i>'"$title"'</i></span>"</label>
				</text>
				<text use-markup="true" space-expand="true" space-fill="true" xalign="0">
				<input file>'$PROGTMP/$Argument'</input>
	</text>
				</vbox>
			</hbox>
		</frame>
		<button image-position="0" use-stock="true" label="gtk-cancel">
			<action type="exit">false</action>
		</button>
		<button image-position="0" use-stock="true" label="gtk-ok">
			<action type="exit">true</action>
		</button>
	</vbox>
	</window>
	'
	$GTKDIALOG --center --program=COMMONDIALOG

# Dialogtype not defined ?
elif [ $DialogType = "" ]
then
		title=Warning
		icon=dialog-$Argument
		color=DarkOrange
	export COMMONDIALOG='
	<window title=" '"$title"' " icon-name="gtk-'"$icon"'">
	<vbox>
		<frame>
			<hbox>
				<pixmap icon_size="6">
					<input file stock="gtk-'$icon'"></input>
				</pixmap>
				<vbox space-expand="true" space-fill="true">
				<text use-markup="true" xalign="0">
				<label>"<span  color='"'$color'"' font-family='"'Sans'"' weight='"'bold'"' size='"'x-large'"'><i>'"$title"'</i></span>"</label>
				</text>
				<text use-markup="true" space-expand="true" space-fill="true" xalign="0">
					Still under development
				</text>
				</vbox>
			</hbox>
		</frame>
		<button image-position="0" use-stock="true" label="gtk-'$icon'">
			<action type="exit">COMMONDIALOG</action>
		</button>
	</vbox>
	</window>
	'
	$GTKDIALOG --center --program=COMMONDIALOG
fi
